package com.hyh.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hyh.admin.domain.guide.IntelligenceGuide;
import com.hyh.admin.domain.guide.vo.QuestionVo;
import com.hyh.admin.domain.resources.Resources;
import com.hyh.admin.mapper.guide.IntelligenceGuideMapper;
import com.hyh.admin.service.IIntelligenceGuideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class IntelligenceGuideServiceImpl implements IIntelligenceGuideService {

    @Autowired
    private IntelligenceGuideMapper intelligenceGuideMapper;

    @Override
    public List<IntelligenceGuide> listQuestion() {
        LambdaQueryWrapper<IntelligenceGuide> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IntelligenceGuide::getOptionType, 'q');
        return intelligenceGuideMapper.selectList(queryWrapper);
    }

    @Override
    public List<IntelligenceGuide> listAnswers(Long parentId) {
        if (parentId == null) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<IntelligenceGuide> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IntelligenceGuide::getParentId, parentId);
        queryWrapper.eq(IntelligenceGuide::getOptionType, 'a');
        return intelligenceGuideMapper.selectList(queryWrapper);
    }

    @Override
    public List<QuestionVo> listQuestionsWithAnswers() {
        List<IntelligenceGuide> questions = listQuestion();
        return questions.stream().map(q -> {
            List<IntelligenceGuide> answers = listAnswers(q.getId());
            return new QuestionVo(q, answers);
        }).collect(Collectors.toList());
    }

    @Override
    public List<Resources> listResourcesByAnswerIds(List<String> answerIds) {
        return intelligenceGuideMapper.selectResourcesByAnswerIds(answerIds);
    }
}
